package org.wyz.observer_after;

import java.util.ArrayList;
import java.util.List;

public class LogPub {

    private List<SubClient> subClientList = new ArrayList<>();

    public void addSubClient(SubClient subClient) {
        subClientList.add(subClient);
    }

    public void removeSubClient(SubClient subClient) {
        subClientList.remove(subClient);
    }

    public String getLog() {
        return "new log";
    }

    public void publishLog() {
        String log = getLog();

        for (SubClient subClient : subClientList) {
            subClient.consume(log);
        }
    }


    public static void main(String[] args) {
        LogPub pub = new LogPub();
        DfsClient dfs = new DfsClient();
        dfs.subscribe(pub);
        MqClient mq = new MqClient();
        dfs.subscribe(pub);

        // 推送
        pub.publishLog();
        System.out.println("------");

        // 取消订阅了再看看
        dfs.unsubscribe(pub);
//        mq.unsubscribe(pub);
        // 推送
        pub.publishLog();
    }
}
